package pers.cnzdy.tutorial.Chapter5_UI.Section3_Event;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import pers.cnzdy.tutorial.R;

public class AnswerActivity extends AppCompatActivity {
    static final String EXTRA_ANSWER = "answer";
    static final String EXTRA_ANSWER_SHOWN = "answer_shown";
    static final String EXTRA_STATEMENT = "statement";

    TextView warningTextView;
    TextView answerTextView;
    Button   showAnswerBtn;
    boolean  answer;
    int      statementId;

    public static Intent newIntent(Context context, int statementId,
                                   boolean answer){
        Intent i = new Intent(context, AnswerActivity.class);
        i.putExtra(EXTRA_ANSWER, answer);
        i.putExtra(EXTRA_STATEMENT, statementId);
        return i;
    }

    public static boolean wasAnswerShown(Intent i) {
        return i.getBooleanExtra(EXTRA_ANSWER_SHOWN, false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);

        answer = getIntent().getBooleanExtra(EXTRA_ANSWER, false);
        statementId = getIntent().getIntExtra(EXTRA_STATEMENT, 0);

        answerTextView = findViewById(R.id.answer_text_view);

//        warningTextView = (TextView) findViewById(R.id.check_warning);
//        warningTextView.setText(statementId);

        showAnswerBtn = findViewById(R.id.show_answer_button);
        showAnswerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (answer) {
                    answerTextView.setText(R.string.true_button);
                } else {
                    answerTextView.setText(R.string.false_button);
                }
                setAnswerShown(true);
            }
        });
    }

    private void setAnswerShown(boolean isAnswerShown) {
        Intent i = new Intent();
        i.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, i);
    }
}
